const express = require('express');
const router = express.Router();
const { 
  addComment, 
  getComments,
  getCommentsByTarget,
  getReplies,
  deleteComment 
} = require('../controllers/commentController');
const { protect } = require('../middlewares/authMiddleware');

/**
 * @swagger
 * components:
 *   schemas:
 *     Comment:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           description: 评论的唯一标识符
 *         content:
 *           type: string
 *           description: 评论内容
 *         targetType:
 *           type: string
 *           enum: ['notice', 'topic', 'activity', 'market', 'post']
 *           description: 评论目标类型
 *         targetId:
 *           type: string
 *           description: 评论目标的ID
 *         userId:
 *           type: string
 *           description: 评论的用户ID
 *         parentId:
 *           type: string
 *           description: 父评论ID（用于回复）
 *         createdAt:
 *           type: string
 *           format: date-time
 *           description: 评论创建时间
 *         updatedAt:
 *           type: string
 *           format: date-time
 *           description: 评论更新时间
 */

/**
 * @swagger
 * /comments/{targetType}/{targetId}:
 *   post:
 *     summary: 添加评论
 *     tags: [Comments]
 *     description: 为指定目标添加评论
 *     parameters:
 *       - in: path
 *         name: targetType
 *         required: true
 *         schema:
 *           type: string
 *           enum: ['notice', 'topic', 'activity', 'market', 'post']
 *         description: 评论目标类型
 *       - in: path
 *         name: targetId
 *         required: true
 *         schema:
 *           type: string
 *         description: 评论目标ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               content:
 *                 type: string
 *                 description: 评论内容
 *     responses:
 *       201:
 *         description: 评论成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Comment'
 *       400:
 *         description: 评论内容不能为空
 *       500:
 *         description: 添加评论失败，请稍后重试
 */
router.post('/:targetType/:targetId', protect, addComment);

/**
 * @swagger
 * /comments/{targetType}/{targetId}:
 *   get:
 *     summary: 获取评论列表
 *     tags: [Comments]
 *     description: 获取指定目标的评论列表
 *     parameters:
 *       - in: path
 *         name: targetType
 *         required: true
 *         schema:
 *           type: string
 *           enum: ['notice', 'topic', 'activity', 'market', 'post']
 *         description: 评论目标类型
 *       - in: path
 *         name: targetId
 *         required: true
 *         schema:
 *           type: string
 *         description: 评论目标ID
 *     responses:
 *       200:
 *         description: 返回评论列表
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Comment'
 *       500:
 *         description: 获取评论失败，请稍后重试
 */
router.get('/:targetType/:targetId', getCommentsByTarget);

/**
 * @swagger
 * /comments/{commentId}/replies:
 *   get:
 *     summary: 获取评论回复列表
 *     tags: [Comments]
 *     description: 获取指定评论的回复列表
 *     parameters:
 *       - in: path
 *         name: commentId
 *         required: true
 *         schema:
 *           type: string
 *         description: 评论ID
 *     responses:
 *       200:
 *         description: 返回回复列表
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Comment'
 *       500:
 *         description: 获取回复失败，请稍后重试
 */
router.get('/:commentId/replies', getReplies);

/**
 * @swagger
 * /comments/{commentId}:
 *   delete:
 *     summary: 删除评论
 *     tags: [Comments]
 *     description: 删除指定评论
 *     parameters:
 *       - in: path
 *         name: commentId
 *         required: true
 *         schema:
 *           type: string
 *         description: 评论ID
 *     responses:
 *       200:
 *         description: 评论已删除
 *       403:
 *         description: 无权删除此评论
 *       404:
 *         description: 评论不存在
 *       500:
 *         description: 删除评论失败，请稍后重试
 */
router.delete('/:commentId', protect, deleteComment);

module.exports = router;